package io.renren.modules.project.service.impl;

import io.renren.modules.oss.constant.UploadConstant;
import io.renren.modules.project.entity.ContactAccessoryEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserTokenEntity;
import io.renren.modules.sys.service.SysUserTokenService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.modules.project.dao.ContactSheetDao;
import io.renren.modules.project.entity.ContactSheetEntity;
import io.renren.modules.project.service.ContactSheetService;

import javax.servlet.http.HttpServletRequest;


@Service("contactSheetService")
public class ContactSheetServiceImpl implements ContactSheetService {
	@Autowired
	private ContactSheetDao contactSheetDao;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	
	@Override
	public ContactSheetEntity queryObject(String id){
		return contactSheetDao.queryObject(id);
	}
	
	@Override
	public List<ContactSheetEntity> queryList(Map<String, Object> map, HttpServletRequest request){
		String getContextPath = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+getContextPath+"/";
		SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		SysUserTokenEntity userToken = sysUserTokenService.queryByUserId(user.getUserId());
		String baseUrl = basePath + UploadConstant.IMAGES_ROUTE + "/";
		List<ContactSheetEntity> contactSheetEntities =  contactSheetDao.queryList(map);
		if (contactSheetEntities != null && contactSheetEntities.size()>0){
			for (int i = 0 ; i <contactSheetEntities.size(); i++){
				if (contactSheetEntities.get(i).getAccessoryUrl() != null && !"".equals(contactSheetEntities.get(i).getAccessoryUrl())){
					String tempPath = StringUtils.substringAfterLast(contactSheetEntities.get(i).getAccessoryUrl(), "/");
					String imageUrl = baseUrl + tempPath + "?token=" + userToken.getToken();
					contactSheetEntities.get(i).setAccessoryUrl(imageUrl);
				}
				if (contactSheetEntities.get(i).getReplyAccessoryUrl() != null && !"".equals(contactSheetEntities.get(i).getReplyAccessoryUrl())){
					String tempPath = StringUtils.substringAfterLast(contactSheetEntities.get(i).getReplyAccessoryUrl(), "/");
					String imageUrl = baseUrl + tempPath + "?token=" + userToken.getToken();
					contactSheetEntities.get(i).setReplyAccessoryUrl(imageUrl);
				}
			}
		}
		return contactSheetEntities;
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return contactSheetDao.queryTotal(map);
	}
	
	@Override
	public void save(ContactSheetEntity contactSheet){
		contactSheetDao.save(contactSheet);
	}
	
	@Override
	public void update(ContactSheetEntity contactSheet){
		contactSheetDao.update(contactSheet);
	}
	
	@Override
	public void delete(String id){
		contactSheetDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		contactSheetDao.deleteBatch(ids);
	}

	@Override
	public void addConferencesheet(ContactSheetEntity contactSheet) {
		//我要获取当前的日期
		Date date = new Date();
		//设置要获取到什么样的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		contactSheet.setCreationTime(sdf.format(date));
		contactSheetDao.save(contactSheet);
	}

	@Override
	public void uploadConferencesheet(ContactAccessoryEntity contactAccessoryEntity) {
		contactSheetDao.uploadConferencesheet(contactAccessoryEntity);
	}

	@Override
	public void uploadReplyAccessory(ContactAccessoryEntity contactAccessoryEntity) {
		contactSheetDao.uploadReplyAccessory(contactAccessoryEntity);
	}

}
